#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2025/4/24 10:57
# @Author  : TomNewChao
# @File    : upload_file_to_obs.py
# @Description :
import os
import sys
import click
import logging
from pathlib import Path

CURR_DIR = Path(__file__).resolve()
ROOT_DIR = CURR_DIR.parent.parent.parent
sys.path.append(ROOT_DIR.as_posix())
from scripts.utils.obs_helper import ObsHelper

logging.basicConfig(level=logging.INFO,
                    format="%(asctime)s  %(levelname)s:%(message)s")


class PipeLineLogHelper:

    def __init__(self, ak, sk, obs_endpoint):
        self._obs_helper = ObsHelper(ak, sk, obs_endpoint)

    def upload_obs(self, bucket_name, object_key, filename):
        self._obs_helper.upload_file(bucket_name, object_key, filename)


@click.command()
@click.option("--ak")
@click.option("--sk")
@click.option("--obs_endpoint")
@click.option("--obs_bucket_name")
@click.option("--obs_key")
@click.option("--filename")
def main(ak, sk, obs_endpoint, obs_bucket_name, obs_key, filename):
    logging.info(f"upload_file_to_obs ak: {ak}, sk: {sk}, obs_endpoint: {obs_endpoint}, obs_bucket_name: {obs_bucket_name}, obs_key: {obs_key}, filename: {filename}")
    logging.info("=" * 20 + "start to upload to obs" + "=" * 20)

    if not all([ak, sk, obs_endpoint, obs_bucket_name, obs_key, filename]):
        logging.info("Lack of params, Please check.")
        sys.exit(-1)

    pipeline_log_helper = PipeLineLogHelper(ak, sk, obs_endpoint)

    # upload the filename
    if os.path.exists(filename):
        logging.info("upload the {} to {} in obs:{}".format(filename, obs_key, obs_bucket_name))
        pipeline_log_helper.upload_obs(obs_bucket_name, obs_key, filename)
    else:
        logging.info("The file:{} is not exists.".format(filename))

    logging.info("=" * 20 + "end to uploaded the obs" + "=" * 20)

if __name__ == '__main__':
    main()
